using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace sharpTV.Entities
{
	/// <summary>
	/// Strongly-typed collection for the User class.
	/// </summary>
    [Serializable]
	public partial class UserCollection : ActiveList<User, UserCollection>
	{	   
		public UserCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>UserCollection</returns>
		public UserCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                User o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the sharpTV_Users table.
	/// </summary>
	[Serializable]
	public partial class User : ActiveRecord<User>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public User()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public User(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public User(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public User(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("sharpTV_Users", TableType.Table, DataService.GetInstance("sharpTVSubSonicProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarUserId = new TableSchema.TableColumn(schema);
				colvarUserId.ColumnName = "UserId";
				colvarUserId.DataType = DbType.Int32;
				colvarUserId.MaxLength = 0;
				colvarUserId.AutoIncrement = true;
				colvarUserId.IsNullable = false;
				colvarUserId.IsPrimaryKey = true;
				colvarUserId.IsForeignKey = false;
				colvarUserId.IsReadOnly = false;
				colvarUserId.DefaultSetting = @"";
				colvarUserId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUserId);
				
				TableSchema.TableColumn colvarUserUId = new TableSchema.TableColumn(schema);
				colvarUserUId.ColumnName = "UserUId";
				colvarUserUId.DataType = DbType.Guid;
				colvarUserUId.MaxLength = 0;
				colvarUserUId.AutoIncrement = false;
				colvarUserUId.IsNullable = true;
				colvarUserUId.IsPrimaryKey = false;
				colvarUserUId.IsForeignKey = false;
				colvarUserUId.IsReadOnly = false;
				
						colvarUserUId.DefaultSetting = @"(newid())";
				colvarUserUId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUserUId);
				
				TableSchema.TableColumn colvarApplicationId = new TableSchema.TableColumn(schema);
				colvarApplicationId.ColumnName = "ApplicationId";
				colvarApplicationId.DataType = DbType.Int32;
				colvarApplicationId.MaxLength = 0;
				colvarApplicationId.AutoIncrement = false;
				colvarApplicationId.IsNullable = true;
				colvarApplicationId.IsPrimaryKey = false;
				colvarApplicationId.IsForeignKey = false;
				colvarApplicationId.IsReadOnly = false;
				colvarApplicationId.DefaultSetting = @"";
				colvarApplicationId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarApplicationId);
				
				TableSchema.TableColumn colvarUsername = new TableSchema.TableColumn(schema);
				colvarUsername.ColumnName = "Username";
				colvarUsername.DataType = DbType.String;
				colvarUsername.MaxLength = 256;
				colvarUsername.AutoIncrement = false;
				colvarUsername.IsNullable = true;
				colvarUsername.IsPrimaryKey = false;
				colvarUsername.IsForeignKey = false;
				colvarUsername.IsReadOnly = false;
				
						colvarUsername.DefaultSetting = @"(N'')";
				colvarUsername.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUsername);
				
				TableSchema.TableColumn colvarPassword = new TableSchema.TableColumn(schema);
				colvarPassword.ColumnName = "Password";
				colvarPassword.DataType = DbType.String;
				colvarPassword.MaxLength = 256;
				colvarPassword.AutoIncrement = false;
				colvarPassword.IsNullable = true;
				colvarPassword.IsPrimaryKey = false;
				colvarPassword.IsForeignKey = false;
				colvarPassword.IsReadOnly = false;
				
						colvarPassword.DefaultSetting = @"(N'')";
				colvarPassword.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPassword);
				
				TableSchema.TableColumn colvarPasswordSalt = new TableSchema.TableColumn(schema);
				colvarPasswordSalt.ColumnName = "PasswordSalt";
				colvarPasswordSalt.DataType = DbType.String;
				colvarPasswordSalt.MaxLength = 256;
				colvarPasswordSalt.AutoIncrement = false;
				colvarPasswordSalt.IsNullable = true;
				colvarPasswordSalt.IsPrimaryKey = false;
				colvarPasswordSalt.IsForeignKey = false;
				colvarPasswordSalt.IsReadOnly = false;
				
						colvarPasswordSalt.DefaultSetting = @"(N'')";
				colvarPasswordSalt.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPasswordSalt);
				
				TableSchema.TableColumn colvarNotificationEmailAddress = new TableSchema.TableColumn(schema);
				colvarNotificationEmailAddress.ColumnName = "NotificationEmailAddress";
				colvarNotificationEmailAddress.DataType = DbType.String;
				colvarNotificationEmailAddress.MaxLength = 256;
				colvarNotificationEmailAddress.AutoIncrement = false;
				colvarNotificationEmailAddress.IsNullable = true;
				colvarNotificationEmailAddress.IsPrimaryKey = false;
				colvarNotificationEmailAddress.IsForeignKey = false;
				colvarNotificationEmailAddress.IsReadOnly = false;
				
						colvarNotificationEmailAddress.DefaultSetting = @"(N'')";
				colvarNotificationEmailAddress.ForeignKeyTableName = "";
				schema.Columns.Add(colvarNotificationEmailAddress);
				
				TableSchema.TableColumn colvarCreationDate = new TableSchema.TableColumn(schema);
				colvarCreationDate.ColumnName = "CreationDate";
				colvarCreationDate.DataType = DbType.DateTime;
				colvarCreationDate.MaxLength = 0;
				colvarCreationDate.AutoIncrement = false;
				colvarCreationDate.IsNullable = true;
				colvarCreationDate.IsPrimaryKey = false;
				colvarCreationDate.IsForeignKey = false;
				colvarCreationDate.IsReadOnly = false;
				
						colvarCreationDate.DefaultSetting = @"(getdate())";
				colvarCreationDate.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreationDate);
				
				TableSchema.TableColumn colvarOwner = new TableSchema.TableColumn(schema);
				colvarOwner.ColumnName = "Owner";
				colvarOwner.DataType = DbType.Guid;
				colvarOwner.MaxLength = 0;
				colvarOwner.AutoIncrement = false;
				colvarOwner.IsNullable = true;
				colvarOwner.IsPrimaryKey = false;
				colvarOwner.IsForeignKey = false;
				colvarOwner.IsReadOnly = false;
				colvarOwner.DefaultSetting = @"";
				colvarOwner.ForeignKeyTableName = "";
				schema.Columns.Add(colvarOwner);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["sharpTVSubSonicProvider"].AddSchema("sharpTV_Users",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("UserId")]
		[Bindable(true)]
		public int UserId 
		{
			get { return GetColumnValue<int>(Columns.UserId); }
			set { SetColumnValue(Columns.UserId, value); }
		}
		  
		[XmlAttribute("UserUId")]
		[Bindable(true)]
		public Guid? UserUId 
		{
			get { return GetColumnValue<Guid?>(Columns.UserUId); }
			set { SetColumnValue(Columns.UserUId, value); }
		}
		  
		[XmlAttribute("ApplicationId")]
		[Bindable(true)]
		public int? ApplicationId 
		{
			get { return GetColumnValue<int?>(Columns.ApplicationId); }
			set { SetColumnValue(Columns.ApplicationId, value); }
		}
		  
		[XmlAttribute("Username")]
		[Bindable(true)]
		public string Username 
		{
			get { return GetColumnValue<string>(Columns.Username); }
			set { SetColumnValue(Columns.Username, value); }
		}
		  
		[XmlAttribute("Password")]
		[Bindable(true)]
		public string Password 
		{
			get { return GetColumnValue<string>(Columns.Password); }
			set { SetColumnValue(Columns.Password, value); }
		}
		  
		[XmlAttribute("PasswordSalt")]
		[Bindable(true)]
		public string PasswordSalt 
		{
			get { return GetColumnValue<string>(Columns.PasswordSalt); }
			set { SetColumnValue(Columns.PasswordSalt, value); }
		}
		  
		[XmlAttribute("NotificationEmailAddress")]
		[Bindable(true)]
		public string NotificationEmailAddress 
		{
			get { return GetColumnValue<string>(Columns.NotificationEmailAddress); }
			set { SetColumnValue(Columns.NotificationEmailAddress, value); }
		}
		  
		[XmlAttribute("CreationDate")]
		[Bindable(true)]
		public DateTime? CreationDate 
		{
			get { return GetColumnValue<DateTime?>(Columns.CreationDate); }
			set { SetColumnValue(Columns.CreationDate, value); }
		}
		  
		[XmlAttribute("Owner")]
		[Bindable(true)]
		public Guid? Owner 
		{
			get { return GetColumnValue<Guid?>(Columns.Owner); }
			set { SetColumnValue(Columns.Owner, value); }
		}
		
		#endregion
		
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid? varUserUId,int? varApplicationId,string varUsername,string varPassword,string varPasswordSalt,string varNotificationEmailAddress,DateTime? varCreationDate,Guid? varOwner)
		{
			User item = new User();
			
			item.UserUId = varUserUId;
			
			item.ApplicationId = varApplicationId;
			
			item.Username = varUsername;
			
			item.Password = varPassword;
			
			item.PasswordSalt = varPasswordSalt;
			
			item.NotificationEmailAddress = varNotificationEmailAddress;
			
			item.CreationDate = varCreationDate;
			
			item.Owner = varOwner;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varUserId,Guid? varUserUId,int? varApplicationId,string varUsername,string varPassword,string varPasswordSalt,string varNotificationEmailAddress,DateTime? varCreationDate,Guid? varOwner)
		{
			User item = new User();
			
				item.UserId = varUserId;
			
				item.UserUId = varUserUId;
			
				item.ApplicationId = varApplicationId;
			
				item.Username = varUsername;
			
				item.Password = varPassword;
			
				item.PasswordSalt = varPasswordSalt;
			
				item.NotificationEmailAddress = varNotificationEmailAddress;
			
				item.CreationDate = varCreationDate;
			
				item.Owner = varOwner;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn UserIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn UserUIdColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn ApplicationIdColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn UsernameColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn PasswordColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn PasswordSaltColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn NotificationEmailAddressColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn CreationDateColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        public static TableSchema.TableColumn OwnerColumn
        {
            get { return Schema.Columns[8]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string UserId = @"UserId";
			 public static string UserUId = @"UserUId";
			 public static string ApplicationId = @"ApplicationId";
			 public static string Username = @"Username";
			 public static string Password = @"Password";
			 public static string PasswordSalt = @"PasswordSalt";
			 public static string NotificationEmailAddress = @"NotificationEmailAddress";
			 public static string CreationDate = @"CreationDate";
			 public static string Owner = @"Owner";
						
		}
		#endregion
		
		#region Update PK Collections
		
        #endregion
    
        #region Deep Save
		
        #endregion
	}
}
